home *** CD-ROM | disk | FTP | other *** search
- {========
- Newsgroups: comp.lang.pascal.delphi.components
- Subject: Free Component: TCenterPanel!!
- From: dhuff@solar.sky.net ()
- Date: 23 Aug 1995 18:23:39 GMT
-
- Greetings,
-
- I've been developing custom components, and would like
- to share the fruits of labor. Following is the source
- code to TCenterPanel.
-
- I had a number of dialogs with Okay and Cancel buttons
- (some just had Cancel,) within a TPanel aligned at the
- bottom of the forms. I wanted to make these buttons
- stay centered when the form (and hence the TPanel) was
- resized. Instead of writing the routine and copying the
- code everywhere I needed it (ala VB), I derived a
- component from TPanel that horizontally centers all
- components within it.
-
- Differences from TPanel:
- BufferSpace Integer property that defines the leftmost
- extent to which the child controls will be moved.
- This is handy because as the panel is sized down,
- the controls may move over the left side of your
- bevel and look cheesy.
-
- Send me a mail if you like it, extend it, learn from it,
- or otherwise use it.
-
- Regards,
-
- Don Huff
- dhuff@sky.net
- }
-
- unit CentrPnl;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, ExtCtrls;
-
- type
- TCenterPanel = class(TPanel)
- private
- { Private declarations }
- FBufferSpace : Integer;
-
- procedure SetBufferSpace(Value: Integer);
- protected
- { Protected declarations }
- procedure Resize; override;
- public
- { Public declarations }
- constructor Create(AOwner : TComponent); override;
- published
- { Published declarations }
- property BufferSpace : Integer read FBufferSpace write SetBufferSpace default 0;
- end;
-
- procedure Register;
-
- implementation
-
- procedure Register;
- begin
- RegisterComponents('Samples', [TCenterPanel]);
- end;
-
- constructor TCenterPanel.Create(AOwner : TComponent);
- begin
- inherited Create(AOwner);
- FBufferSpace := 0; {init default}
- end; {Create}
-
- procedure TCenterPanel.Resize;
- var
- CenterLeft, {the space needed on the left to exactly center all child controls}
- ControlLeft, {placeholder for left edge value of current control}
- ControlRight, {placeholder for right edge value of current control}
- DeltaLeft, {amount we need to add to each control's left property to center it}
- I, {general purpose index for our loops}
- MaxControlIdx, {the maximum valid index in the Controls array}
- MaxRight, {the rightmost point of all child controls}
- MinLeft : Integer; {the leftmost point of all child controls}
- begin
- inherited ReSize; {do default handling}
-
- MaxControlIdx := ControlCount - 1;
-
- if MaxControlIdx >= 0 then begin
- {Get the leftmost point and rightmost point of all controls}
- MinLeft := Controls[0].Left;
- MaxRight := MinLeft + Controls[0].Width;
-
- for I := 1 to MaxControlIdx do begin
- ControlLeft := Controls[I].Left;
- if ControlLeft < MinLeft then
- MinLeft := ControlLeft;
-
- ControlRight := ControlLeft + Controls[I].Width;
- if ControlRight > MaxRight then
- MaxRight := ControlRight;
-
- end;
-
- {figure space on left side to exactly center all controls
- Note: may be negative (panel is smaller than controls)}
- CenterLeft := (Width - MaxRight + MinLeft) div 2;
-
- if CenterLeft < FBufferSpace then
- {if amount to center is smaller than FBufferSpace, move to FBufferSpace}
- DeltaLeft := FBufferSpace - MinLeft
- else
- {if amount to center is greater than FBufferSpace, move to CenterLeft}
- DeltaLeft := CenterLeft - MinLeft;
-
- {adjust all controls the same amount}
- for I := 0 to MaxControlIdx do
- Controls[I].Left := Controls[I].Left + DeltaLeft;
-
- end; {if MaxControlIdx >= 0}
-
- end; {Resize}
-
- procedure TCenterPanel.SetBufferSpace(Value: Integer);
- begin
- {do not allow negative values}
- if Value < 0 then
- Value := 0;
-
- FBufferSpace := Value;
-
- end; {SetBufferSpace}
-
- end.
-
-